package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.CategoryBrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    public PageResult<BrandDTO> findByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
//        1、关键字查询  select * from tb_brand where name like '%H%' or letter ='H' order by name desc
//        Brand brand = new Brand();
//        brand.setName("%"+key+"%");  and
//        brand.setLetter(key);

        Example example = new Example(Brand.class); //用来构建复杂查询
        Example.Criteria criteria = example.createCriteria();

//        判断key是否有值
//        select * from tb_brand where name like '%H%' or letter ='H'
        if(StringUtils.isNotBlank(key)){
            criteria.orLike("name","%"+key+"%");
            criteria.orEqualTo("letter",key);
        }
//        sortBy 根据XX字段排序
        example.setOrderByClause(sortBy+(desc?" desc":" asc"));
        PageHelper.startPage(page,rows); //分页
        List<Brand> brandList = brandMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(brandList);

//        把brandList装成brandDTOList
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(brandList, BrandDTO.class);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),brandDTOList);//Long total, List<BrandDTO> items
        return pageResult;

    }

    @Transactional
    public void save(BrandDTO brandDTO, List<Long> cids) {
//        把DTO转成数据保存时的brand对象
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);  //brand有三个属性 name  image letter
//        插入品牌数据   通用mapper插入数据后会直接返回id
        brandMapper.insertSelective(brand);
        Long bid = brand.getId();

//        插入分类和品牌的中间表的数据
//        cids商品分类的ids
        CategoryBrand categoryBrand = null;
        for (Long cid : cids) {
            categoryBrand = new CategoryBrand(cid,bid);
            categoryBrandMapper.insert(categoryBrand);
//        insert tb_category_brand(category_id,brand_id) values (cid,bid)
        }
    }

    public void update(BrandDTO brandDTO, List<Long> cids) {
        //        把DTO转成数据保存时的brand对象
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);  //brand有三个属性 name  image letter
//        插入品牌数据   通用mapper插入数据后会直接返回id
        brandMapper.updateByPrimaryKeySelective(brand);
        Long bid = brand.getId();

//        把之前的品牌和分类的数据删除
//        delete from xxx where brand_id=??
        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setBrandId(bid);
        categoryBrandMapper.delete(categoryBrand);
//        插入分类和品牌的中间表的数据
//        cids商品分类的ids
        for (Long cid : cids) {
            categoryBrand = new CategoryBrand(cid,bid);
            categoryBrandMapper.insert(categoryBrand);
//        insert tb_category_brand(category_id,brand_id) values (cid,bid)
        }
    }

    public List<BrandDTO> findBrandByCategoryId(Long id) {
        List<Brand>  brandList = brandMapper.findBrandByCategoryId(id);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList,BrandDTO.class);

    }

    public List<BrandDTO> findBrandByIds(List<Long> ids) {
        List<Brand>  brandList = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList,BrandDTO.class);
    }

    public BrandDTO findBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }
}

